<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/static/lib/layui-v2.6.3/css/layui.css}" media="all">
    <script type="text/javascript" th:src="@{/static/lib/layui-v2.6.3/layui.js}"></script>
    <script type="text/javascript" th:src="@{/static/lib/jquery-3.4.1/jquery-3.4.1.min.js}"></script>
    <script type="text/javascript" th:src="@{/static/js/jquery-1.9.1.min.js}"></script>
</head>
<body>
<form action="downGoods" method="post">
    <button id="que" type="submit" style="display:none">隐藏的查询按钮</button>
</form>
<form id="ffff" action="downGoods" method="post" class="layui-form">
    <input type="hidden" name="goodsStatus" value="1">
    <div class="layui-form-item" style="display: inline-block;width: 300px;">
        <label class="layui-form-label">商品名查询:</label>
        <div class="layui-input-block">
            <input type="text" name="goodsName" placeholder="商品名查询" th:value="${goo.goodsName}"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item"
         style="display: inline-block; margin-top: 10px; position: relative; left: -20px; width: 300px;">
        <label class="layui-form-label">分类查询:</label>
        <div class="layui-input-block">
            <select name="superiorId" lay-verify="required">
                <option value="">商品分类</option>
                <span th:each="cg:${cs}">
							<option th:value="${cg.id}" th:text="${cg.classificationName}"
                                    th:selected="${goo.superiorId eq cg.id}"></option>
						</span>
            </select>
        </div>
    </div>
    <button id="btn1" type="submit" class="layui-btn" style="display: inline-block; left: 10px;">查询</button>
</form>
<button onclick="getAllChecked()" type="button" class="layui-btn layui-btn-primary"
        style="display: inline-block; left: 10px;">一键上架</button>

<table class="layui-table">
    <colgroup>
        <col>
        <col width="150">
        <col width="150">
    </colgroup>
    <thead>
    <tr>
        <th>
            <input type="checkbox" onclick="checkAll(this)">
        </th>
        <th style="font-weight:bolder;">商品名称</th>
        <th style="font-weight:bolder;">图片</th>
        <th style="font-weight:bolder;">单价</th>
        <th style="font-weight:bolder;">库存</th>
        <th style="font-weight:bolder;">所属分类</th>
        <th style="font-weight:bolder;">操作</th>
    </tr>
    </thead>
    <tbody>
    <span th:each="goods:${pageInfo.list}">
					<tr>
						<td>
							<input type="checkbox" th:value="${goods.id}" name="k">
						</td>
						<td th:text="${goods.goodsName}"></td>
						<td><img th:src="${'http://localhost:8887/'+ goods.imgUrl}" width="150px" height="100px"></td>
						<td th:text="${goods.goodsPrice}"></td>
						<td th:text="${goods.goodsCount}"></td>
						<td th:text="${goods.categoryName}"></td>
						<td>
							<button th:onclick="update([[${goods.id}]])" class="layui-btn layui-btn-xs">更新</button>
							<button th:onclick="del([[${goods.id}]])"
                                    class="layui-btn layui-btn-danger layui-btn-xs">删除</button>
						</td>
					</tr>
				</span>
    </tbody>
</table>
<div style="text-align:center;">
    <button class="layui-btn" onclick="jump(1)">首页</button>
    <button class="layui-btn" th:onclick="jump([[${pageInfo.pageNum-1}]])">上一页</button>
    <span th:text="${pageInfo.pageNum} + '/' + ${pageInfo.pages} + '页'"></span>
    <button class="layui-btn" th:onclick="jump([[${pageInfo.pageNum+1}]])">下一页</button>
    <button class="layui-btn" th:onclick="jump([[${pageInfo.pages}]])">尾页</button>
</div>
</body>
<script>
		function del(id) {
			if (confirm("确定要删除该信息吗？删除将不能恢复！")) {
				$.ajax({
					url: "del",
					type: "post",
					data: "id=" + id,
					//请求发送完之后的回调函数
					success: function(info) {
						console.info(info);
						if (info == 1) {
							alert("删除成功！");
						} else {
							alert("删除失败！");
						}
						$("#btn1").click();
					}
				});
				return true;
			} else
				return false;
		}

		if ([[${pageInfo.pages}]] == 0) {
			alert("没有找到！");
			$("#que").click();
		}

		function jump(pageNum) {
			var params = $("#ffff").serialize();
			var url = "downGoods?" + params + "&pageNum=" + pageNum;
			window.location.href = url;
		}

		function update(id) {
			//弹出窗口
			layer.open({
				title: "修改信息",
				area: ['700px', '300px'],
				type: 2,
				content: "/goods/toUpdate?id=" + id,
			})

		}

		//全选中或不选中
		function checkAll(obj) {
			var a = $(obj).prop("checked"); //true 和 false
			if (a) {
				$(":checkbox[name='k']").prop("checked", true);
			} else {
				$(":checkbox[name='k']").prop("checked", false);
			}
		}
		//一键下架
		function getAllChecked() {
			var es = $(":checkbox[name='k']:checked");
			var params = "";
			$.each(es, function(index, item) {
				console.info(item.value);
				params += item.value + ",";
			})
			if (params.length == 0) {
				alert("请选中上架商品！");
				$("#btn1").click();
			} else {
				window.location.href = "above?ids=" + params.substr(0, params.length - 1);
				alert("上架成功！");

			}
		}
	</script>

</html>
